package com.fll.common;

import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

/**
 *
 * 配置文件
 *
 * @author fanlei
 * @version 1.0
 * @date 2019年01月25日
 */
public class Config {

    //默认配置文件名称
    private static final String DEFAULT_CONF = "config.properties";

    private static Map<String,Config> instances = new ConcurrentHashMap();

    /**
     * 配置文件key-value
     */
    private Properties configuration = new Properties();


    private Config() {
        this.initConfig(Config.DEFAULT_CONF);
    }


    private Config(String configFile) {
        this.initConfig(configFile);
    }

    /**
     * 初始化读取配置文件
     * @param configFile
     */
    private void initConfig(String configFile) {
        InputStream is = Config.class.getClassLoader().getResourceAsStream(configFile);
        try {
            this.configuration.load(is);
            is.close();
        } catch (IOException var4) {
            throw new RuntimeException(var4);
        }
    }

    public static Config getInstance() {
        return getInstance(Config.DEFAULT_CONF);
    }


    public static Config getInstance(String keyConfigFile) {
        Config config = (Config)instances.get(keyConfigFile);
        //map中是否有Config
        if (config == null) {
            Map var2 = instances;
            synchronized(instances) {
                config = (Config)instances.get(keyConfigFile);
                if (config == null) {
                    //再初始化一个配置文件信息
                    config = new Config(keyConfigFile);
                    instances.put(keyConfigFile, config);
                }
            }
        }

        return config;
    }

    /* ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓获取配置↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓ */

    public String getStringValue(String key) {
        return this.configuration.getProperty(key);
    }

    public String getStringValue(String key, String defaultValue) {
        String value = this.getStringValue(key);
        return value == null ? defaultValue : value;
    }

    public int getIntValue(String key, int defaultValue) {
        return LangUtil.parseInt(this.configuration.getProperty(key), defaultValue);
    }

    public int getIntValue(String key) {
        return LangUtil.parseInt(this.configuration.getProperty(key));
    }

    public double getDoubleValue(String key, Double defaultValue) {
        return LangUtil.parseDouble(this.configuration.getProperty(key), defaultValue);
    }

    public double getDoubleValue(String key) {
        return LangUtil.parseDouble(this.configuration.getProperty(key));
    }

    public double getLongValue(String key, Long defaultValue) {
        return (double)LangUtil.parseLong(this.configuration.getProperty(key), defaultValue);
    }

    public double getLongValue(String key) {
        return (double)LangUtil.parseLong(this.configuration.getProperty(key));
    }

    public Boolean getBooleanValue(String key, Boolean defaultValue) {
        return LangUtil.parseBoolean(this.configuration.getProperty(key), defaultValue);
    }

    public Boolean getBooleanValue(String key) {
        return LangUtil.parseBoolean(this.configuration.getProperty(key));
    }

    /* ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑获取配置↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ */



}
